<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\BalanceCard;
use app\massage\model\BalanceOrder;
use app\massage\model\ChannelCate;
use app\massage\model\ChannelList;
use app\massage\model\CoachLevel;
use app\massage\model\Commission;
use app\massage\model\Coupon;
use app\massage\model\Identity;
use app\massage\model\User;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Cap;
use app\shop\model\Date;
use app\shop\model\MsgConfig;
use app\shop\model\OrderAddress;
use app\shop\model\OrderGoods;
use app\shop\model\RefundOrder;
use app\shop\model\RefundOrderGoods;
use app\shop\model\Wallet;
use think\App;
use app\shop\model\Order as Model;
use think\facade\Db;


class AdminChannel extends AdminRest
{


    protected $model;

    protected $cate_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new ChannelList();

        $this->cate_model = new ChannelCate();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 19:09
     * @功能说明:类目列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $this->cate_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-08-30 14:54
     * @功能说明:渠道商下拉
     */
    public function cateSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $data = $this->cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }
    /**
     * @author chenniang
     * @DataTime: 2022-08-30 10:53
     * @功能说明:添加类目
     */
    public function cateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->cate_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-30 10:53
     * @功能说明:添加类目
     */
    public function cateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $res = $this->cate_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-30 10:56
     * @功能说明:分类详情
     */
    public function cateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->cate_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-30 14:54
     * @功能说明:渠道商下拉
     */
    public function channelSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','in',[2,3]];

        $data = $this->model->where($dis)->field('id,user_name')->select()->toArray();

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-08-30 11:30
     * @功能说明:渠道商列表
     */
    public function channelList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','=',$this->_user['id']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.user_name','like','%'.$input['name'].'%'];

            $where[] = ['a.mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$input['limit'],$where);

        $list = [

            0=>'all',

            1=>'ing',

            2=>'pass',

            4=>'nopass'
        ];

        foreach ($list as $k=> $value){

            $dis_s = [];

            $dis_s[] =['uniacid','=',$this->_uniacid];

            if(!empty($k)){

                $dis_s[] = ['status','=',$k];

            }else{

                $dis_s[] = ['status','>',-1];

            }
            //是否是代理商
            if($this->_user['is_admin']==0){

                $dis_s[] = ['admin_id','=',$this->_user['id']];
            }

            $data[$value] = $this->model->where($dis_s)->count();

        }

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2022-08-03 11:53
     * @功能说明:
     */
    public function channelInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $info = $this->model->dataInfo($dis);

        $user_model = new User();

        $info['nickName'] = $user_model->where(['id'=>$info['user_id']])->value('nickName');

        return $this->success($info);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-07-03 00:15
     * @功能说明:审核(2通过,3取消,4拒绝)
     */
    public function channelUpdate(){
        $input = $this->_input;
        $diss = [
            'id' => $input['id']
        ];
        $info = $this->model->dataInfo($diss);
        if(!empty($input['status'])&&in_array($input['status'],[2,4,-1])){
            $input['sh_time'] = time();
        }
        //删除需要判断佣金提现
        if(isset($input['status'])&&$input['status']==-1){
            if($info['cash']>0){
                $this->errorMsg('还有佣金未提现');
            }
            $dis = [
                'top_id'  => $input['id'],
                'status'  => 1,
                'type'    => 10
            ];
            $cash_model = new Commission();
            $cash = $cash_model->dataInfo($dis);
            if(!empty($cash)){
                $this->errorMsg('还有佣金未到账');
            }
            $dis = [
                'coach_id'=> $input['id'],
                'status'  => 1,
                'type'    => 5
            ];
            $wallet_model = new \app\massage\model\Wallet();
            $wallet = $wallet_model->dataInfo($dis);
            if(!empty($wallet)){
                $this->errorMsg('还有提现未处理');
            }
        }
        $data = $this->model->dataUpdate($diss,$input);
        $identity_model = new Identity();
        $identity = $identity_model->dataInfo(['id' => $info['identity_id']]);
        if($input['status'] == 2 && $identity['price'] > 0){
            //添加代理商身份
            $res = $this->adminAdd($info['user_id']);
            if(true !== $res){
                $this->errorMsg($res);
            }
        }
        $user_model = new User();
        $level_model = new CoachLevel();
        if($input['status'] == 2){
            $level_info = $level_model->dataInfo(['star' => 4]);
            $update = ['identity_id' => $info['identity_id']];
            if(!empty($level_info)){
                //修改星级
                $update['coach_level_id'] = $level_info['id'];
            }
            $user_model->dataUpdate(['id' => $info['user_id']],$update);
        }
        return $this->success($data);
    }

    public function adminAdd($user_id)
    {
        $user_model = new User();
        $user = $user_model->dataInfo(['id' => $user_id]);
        $username = $user['phone'] ?? $user['nickName'];
        if(empty($username)){
            $username = 'admin'.uniqid();
        }
        $data = [
            'username' => $username,
            'uniacid' => $this->_uniacid,
            'is_admin' => 0,
            'passwd_text' => 123456,
            'user_id' => $user_id,
            'agent_name' => $username,
            'salesman_auth' => 1,
            'channel_auth' => 1,
            'partner_auth' => 1,
        ];
        $admin_model = new \app\massage\model\Admin();
        //        $check = $admin_model->jionAdminCheck($data);
        //        if(!empty($check['code'])){
        //            return $check['msg'];
        //        }
        $data['passwd']  = checkPass($data['passwd_text']);
        $dis[] = ['user_id','=',$user_id];
        $dis[] = ['is_admin','=',0];
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status','>',-1];
        $find = $admin_model->where($dis)->find();
        if(empty($find)){
            $admin_model->dataAdd($data);
        }
        $RoleAdminModel = new \app\mobilenode\model\RoleAdmin();
        $node = 'shopOrder,shopBellOrder,shopRefuseOrder,shopRefund,shopBellRefund';
        $role_data = [
            'uniacid' => $this->_uniacid,
            'node' => $node,
            'user_id' => $user_id,
            'mobile' => $user['phone'] ?? '',
            'create_time' => time(),
        ];
        $dis[] = ['user_id','=',$user_id];
        $dis[] = ['is_admin','=',0];
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status','>',-1];
        $info = $admin_model->where($dis)->find();
        if(empty($info)){
            $RoleAdminModel->dataAdd($role_data);
        }
        return true;
    }

    function jionAdminCheck($input){

        if(!empty($input['username']&&$input['username']=='admin')){
            return ['code'=>500,'msg'=>'用户名不能和超管账号相同'];
        }
        $dis[] = ['user_id','=',$input['user_id']];
        $dis[] = ['is_admin','=',0];
        $dis[] = ['uniacid','=',$input['uniacid']];
        $dis[] = ['status','>',-1];
        if(!empty($input['id'])){
            $dis[] = ['id','<>',$input['id']];
        }
        $admin_model = new \app\massage\model\Admin();
        $find = $admin_model->where($dis)->find();

        if(!empty($find)){
            return ['code'=>500,'msg'=>'该用户已经绑定过加盟商'];
        }
        $where[] = ['username','=',$input['username']];
        $where[] = ['uniacid','=',$input['uniacid']];
        $where[] = ['status','>',-1];
        if(!empty($input['id'])){
            $where[] = ['id','<>',$input['id']];
        }
        $find = $admin_model->where($where)->find();
        if(!empty($find)){
            return ['code'=>500,'msg'=>'已经有该用户名的账户'];
        }

        return true;

    }

}
